package com.yanqu.road.logic.activity.tomb.config.entity;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.yanqu.road.pb.tomb.Tomb;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.serialize.*;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ActivityTombEvent {
    private int activityId;
    private int id;

    private int eventType;

    @JsonDeserialize(using = StringToListInt.class)
    private List<Integer> param1;
    @JsonDeserialize(using = StringToListListInt.class)
    private List<List<Integer>> param2;

    @JsonDeserialize(using = StringToPropertyList.class)
    private List<Property> param3;

    @JsonDeserialize(using = StringToListInt.class)
    private List<Integer> param4;
    @JsonDeserialize(using = StringToListListInt.class)
    private List<List<Integer>> reinforcements;

    private int consumeEnergy;

    private int weights;
    private int area;
    private int npcId;
    private int npcBookmark;
    @JsonDeserialize(using = StringToListInt.class)
    private List<Integer> score;

    @JsonDeserialize(using = StringToProperty.class)
    private Property reward;

    @JsonDeserialize(using = StringToItemWeightList.class)
    private List<List<ItemWeight>> monsterReward;

    public int getActivityId() {
        return activityId;
    }

    public void setActivityId(int activityId) {
        this.activityId = activityId;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getEventType() {
        return eventType;
    }

    public void setEventType(int eventType) {
        this.eventType = eventType;
    }

    public List<Integer> getParam1() {
        return param1;
    }

    public void setParam1(List<Integer> param1) {
        this.param1 = param1;
    }

    public List<List<Integer>> getParam2() {
        return param2;
    }

    public void setParam2(List<List<Integer>> param2) {
        this.param2 = param2;
    }

    public List<List<Integer>> getReinforcements() {
        return reinforcements;
    }

    public void setReinforcements(List<List<Integer>> reinforcements) {
        this.reinforcements = reinforcements;
    }

    public int getWeights() {
        return weights;
    }

    public void setWeights(int weights) {
        this.weights = weights;
    }

    public int getArea() {
        return area;
    }

    public void setArea(int area) {
        this.area = area;
    }

    public int getNpcId() {
        return npcId;
    }

    public void setNpcId(int npcId) {
        this.npcId = npcId;
    }


    public Property getReward() {
        return reward;
    }

    public void setReward(Property reward) {
        this.reward = reward;
    }


    public int getNpcBookmark() {
        return npcBookmark;
    }

    public void setNpcBookmark(int npcBookmark) {
        this.npcBookmark = npcBookmark;
    }

    public List<Property> getParam3() {
        return param3;
    }

    public void setParam3(List<Property> param3) {
        this.param3 = param3;
    }

    public int getConsumeEnergy() {
        return consumeEnergy;
    }

    public void setConsumeEnergy(int consumeEnergy) {
        this.consumeEnergy = consumeEnergy;
    }


    public List<Integer> getScore() {
        return score;
    }

    public void setScore(List<Integer> score) {
        this.score = score;
    }

    public List<Integer> getParam4() {
        return param4;
    }

    public void setParam4(List<Integer> param4) {
        this.param4 = param4;
    }

    public Tomb.EventConfig.Builder getEventConfigBuilder() {
        Tomb.EventConfig.Builder builder = Tomb.EventConfig.newBuilder();
        builder.setEventId(this.getId());
        builder.setConsumeEnergy(this.getConsumeEnergy());
        builder.setNpcId(this.getNpcId());
        builder.addAllParam1(this.param1);
        builder.setParam3(PropertyHelper.parsePropertyToString(this.param3, "|"));
        builder.setReward(PropertyHelper.parsePropertyToString(this.reward));
        builder.setArea(this.area);
        return builder;
    }

    public boolean isBigEvent() {
        if (this.area == 9) {
            return true;
        }
        return false;
    }

    public boolean isPvp() {
        if (this.eventType == 5) {
            return true;
        }
        return false;
    }

    public boolean isElite() {
        if (this.eventType == 3) {
            return true;
        }
        return false;
    }

    public boolean isFinalBoss() {
        if (this.eventType == 4) {
            return true;
        }
        return false;
    }


    public List<List<ItemWeight>> getMonsterReward() {
        return monsterReward;
    }

    public void setMonsterReward(List<List<ItemWeight>> monsterReward) {
        this.monsterReward = monsterReward;
    }

    public Property randMonsterReward() {
        List<Item> list = new ArrayList<>();
        for (List<ItemWeight> itemWeightList : this.monsterReward) {
            int total = 0;
            for (ItemWeight itemWeight : itemWeightList) {
                total = total + itemWeight.getWeight();
            }

            Random random = new Random();
            int i = random.nextInt(total);

            for (ItemWeight itemWeight : itemWeightList) {
                if (i < itemWeight.getWeight()) {
                    list.add(itemWeight.getItem());
                    break;
                } else {
                    i = i - itemWeight.getWeight();
                }
            }
        }
        Property property = new Property();
        for (Item item : list) {
            property.addProperty(item.getId(), BigInteger.valueOf(item.getNum()));
        }
        return property;
    }


}
